DicManager = Ext
		.extend(
				Ext.Panel,
				{
					constructor : function(a) {
						Ext.applyIf(this, a);
						this.initUIComponents();
						DicManager.superclass.constructor.call(this, {
							id : "DicManager",
							height : 450,
							autoScroll : true,
							layout : "border",
							title : "数据字典分类管理",
							iconCls : "menu-dictionary",
							items : [ this.leftPanel, this.centerPanel ]
						});
					},
					initUIComponents : function() {
						this.leftPanel = new zhixiang.ux.TreePanelEditor({
							region : "west",
							id : "dicType",
							title : "数据字典分类",
							collapsible : true,
							split : true,
							height : 450,
							width : 160,
							autoScroll : true,
							url : __ctxPath + "/system/getTreeDicType.do?Q_isSys_SN_EQ=0&isPage=0",
							onclick : function(f) {
								this.selectedNode = f;
								var g = f.id;
								var e = Ext.getCmp("dicGrid");
								if (e != null) {
									if (g == 0) {
										e.setTitle("所有数据字典");
									} else {
										e.setTitle(f.text + "-数据字典");
									}
									var d = e.getStore();
									d.proxy = new Ext.data.HttpProxy({
										url:__ctxPath+ "/system/listDicDetail.do?Q_dicType.id_L_EQ="+g
									});
									d.baseParams = {
										parentId : g
									};
									d.reload();
								}
							},
							contextMenuItems : [
									{
										text : "新建分类",
										scope : this,
										iconCls : "btn-add",
										handler : function() {
											var d = Ext.getCmp("dicType");
											var e = new DicTypeForm({
												callback : function() {
													d.root.reload();
												}
											});
											e.show();
										}
									},
									{
										text : "修改分类",
										scope : this,
										iconCls : "btn-edit",
										handler : function() {										    
											var e = Ext.getCmp("dicType");
											var d = e.selectedNode.id;
											if(d>0){
											    var f = new DicTypeForm({
											        dicTypeId : d,
	                                                callback : function() {
	                                                    Ext.getCmp("dicType").root
	                                                            .reload();
	                                                }
	                                            });
	                                            f.show(); 
											}else{
											   
                                                    Ext.ux.Toast.msg(
                                                            "操作信息",
                                                            "请选中一个字典分类！");
                                                    return;
                                                
											}
											
										}
									} ]
						});
						
						this.store = new Ext.data.Store({
							proxy : new Ext.data.HttpProxy({
								url : __ctxPath + "/system/listDicDetail.do?Q_detailId_L_EQ=-1"
							}),
							reader : new Ext.data.JsonReader({
								root : "result",
								totalProperty : "totalCounts",
								fields : [ {
									name : "detailId",
									type : "int"
								}, "codeNo", "codeName","remark"]
							}),
							remoteSort : true
						});
//						this.store = new Ext.data.JsonStore({
//							url : __ctxPath + "/system/listDicDetail.do",
//							root : "result",
//							totalProperty : "totalCounts",
//							remoteSort : true,[
//							fields : [ {
//								name : "detailId",
//								type : "int"
//							}, "codeNo", "codeName"]
//						});
						this.store.setDefaultSort("detailId", "desc");
						this.store.load({
							params : {
								start : 0,
								limit : 25
							}
						});
						var c = new Ext.grid.CheckboxSelectionModel();
						var a = new Ext.grid.ColumnModel({
							columns : [ c, new Ext.grid.RowNumberer(), {
								header : "ID",
								dataIndex : "detailId",
								hidden : true
							},{
								header : "编号",
								dataIndex : "codeNo",
								width : 50
//								editor : new Ext.form.TextField()
							}, {
								header : "名字",
								dataIndex : "codeName",
								width : 50
//								editor : new Ext.form.TextField()
							},{
                                header : "备注",
                                dataIndex : "remark",
                                width : 100
//                              editor : new Ext.form.TextField()
                            },{
	                            header : "管理",
	                            dataIndex : "codeId",
	                            sortable : false,
	                            width : 60,
	                            renderer : function(j, i, g, m, h) {
	                                var l = g.data.detailId;
	                                var k = "";
	                                if (l != 0) {
	                                    if (isGranted("_SystemDicDetailDel")) {
	                                        k += '<button title="删除" value=" " class="btn-delete" onclick="DicManager.remove('
	                                                + l + ')"></button>';
	                                    }
	                                    if (isGranted("_SystemDicDetailEdit")) {
	                                        k += '&nbsp;<button title="编辑" value=" " class="btn-edit" onclick="DicManager.edit('
	                                            + l + ')"></button>';
	                                    }                                   
	                                }
	                                return k;
	                            }
	                        } ],
							defaults : {
								sortable : true,
								menuDisabled : false,
								width : 100
							}
						});
						var b = new Ext.Toolbar(
								{
									items : [
											{
												text : "添加数字字典",
												iconCls : "btn-add",
												scope : this,
												handler : function() {
													var e = this.centerPanel;
													var f = Ext.getCmp("dicType");
													var g = f.selectedNode;
													var h = 0;
													if (g != null) {
														h = g.id;
													}
													if (h == 0) {
														Ext.ux.Toast.msg(
																"操作信息",
																"请从左选择字典分类");
														return;
													}
													new DicDetailForm({
														parentId : h,
														callback : function() {
															e.getStore().reload();
														}
													}).show();
												}
											},
											"-",
											/*{
												xtype : "button",
												text : "保存",
												iconCls : "btn-save",
												scope : this,
												handler : function() {
													var h = [];
													var g = this.centerPanel;
													var e = g.getStore();
													for (var f = 0; f < e
															.getCount(); f += 1) {
														var d = e.getAt(f);
														if (d.dirty) {
															h.push(d.data);
														}
													}
													if (h.length == 0) {
														Ext.ux.Toast.msg("信息",
																"没有对数据进行任何更改");
														return;
													}
													Ext.Ajax
															.request({
																method : "post",
																url : __ctxPath
																		+ "/system/mulSaveDictionary.do",
																params : {
																	data : Ext
																			.encode(h)
																},
																success : function(
																		i) {
																	Ext.ux.Toast
																			.msg(
																					"操作信息",
																					"成功设置");
																	e.reload();
																	g
																			.getView()
																			.refresh();
																},
																failure : function(
																		i) {
																	Ext.ux.Toast
																			.msg(
																					"操作信息",
																					"设置出错，请联系管理员!");
																}
															});
												}
											},
											"-",*/
											{
												text : "删除",
												iconCls : "btn-delete",
												scope : this,
												handler : function() {
													var f = this.centerPanel;
													var d = f
															.getSelectionModel()
															.getSelections();
													if (d.length == 0) {
														Ext.ux.Toast.msg("信息",
																"请选择要删除的记录！");
														return;
													}
													//删除grid 取ids
													var ids =[];
													var k="";
													for (var i = 0; i < d.length; i++) {
														if(d[i].data.detailId>0){
														    ids.push(d[i].data.detailId)
														}else{
														    k += d[i].data.codeName + ",";
														}
														
													}
													if(ids.length>0){
													    DicManager.remove(ids);
													}
													if(k){
													    Ext.MessageBox.show({
								                            title : "操作提示",
								                            msg : k.substr(0,k.length-1)+"id为空",
								                            buttons : Ext.MessageBox.OK,
								                            icon : "ext-mb-error"
								                        });
													}
													
												}
											}]
								});
						this.centerPanel = new Ext.grid.EditorGridPanel({
							region : "center",
							title : "数字字典管理",
							tbar : b,
							clicksToEdit : 1,
							id : "dicGrid",
							store : this.store,
							sm : c,
							cm : a,
							height : 450,
							viewConfig : {
								forceFit : true,
								autoFill : true
							},
							bbar : new Ext.PagingToolbar({
								pageSize : 25,
								store : this.store,
								displayInfo : true,
								displayMsg : "当前页记录索引{0}-{1}， 共{2}条记录",
								emptyMsg : "当前没有记录"
							})
						});
//						  this.centerPanel.addListener("rowdblclick", d);						    
//						    function d(g, f, h) {
//						        g.getSelectionModel().each(function(j) {
//						            var i = j.data.detailId;
//						            if (isGranted("_SystemDicDetailEdit") && i > 0) {
//						                DicManager.edit(i);
//						            }
//						        });
//						    }
					}
				});
DicManager.remove = function(b) {
    var agrid = Ext.getCmp("dicGrid");
    Ext.Msg.confirm("信息确认", "您确认要删除所选记录吗？", function(c) {
        if (c == "yes") {
            Ext.Ajax.request({
                url : __ctxPath
                + "/system/deleteDicDetail.do",
                params : {
                    ids : b
                },
                method : "post",
                success : function(a) {
                    var result=Ext.util.JSON.decode(a.responseText); 
                    if(result.success){
                        Ext.ux.Toast.msg("信息", "成功删除所选记录！");
                        agrid.getStore().reload({
                            params : {
                                start : 0,
                                limit : 25
                            }
                        });
                    }else{ 
                        Ext.MessageBox.show({
                            title : "操作提示",
                            msg : result.msg,
                            buttons : Ext.MessageBox.OK,
                            icon : "ext-mb-error"
                        });
                    }
                }
            });
        }
    });
};
DicManager.edit = function(a) {
    new DicDetailForm({
        detailId : a
    }).show();

};